﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using Gumini.JulaDB.Enums;

namespace Gumini.JulaDB.DataTypes
{
    public class BooleanDataType : DataType
    {
        public BooleanDataType()
            : base(JulaDbType.Boolean, DbType.Boolean, typeof(bool)) { }

        public override int Compare(object a, object b)
        {
            bool aBool = (bool)a;
            bool bBool = (bool)b;
            if (aBool)
            {
                if (bBool) return 0;
                else return 1;
            }
            else
            {
                if (bBool) return -1;
                else return 0;
            }
        }

        protected override object ConvertCore(object source)
        {
            var convertible = source as IConvertible;
            return convertible.ToBoolean(CultureInfo.InvariantCulture);
        }

        public override IEnumerable<string> Names
        {
            get { yield return "bool"; }
        }

        public override IEnumerable<Type> SourceTypes
        {
            get { yield return typeof(bool); }
        }

        public override string ToString()
        {
            return "BOOL";
        }
    }
}
